﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LSQL;
using Sunny.UI;
using Org.BouncyCastle.Tls;

namespace Shopfloormanagement.Forms
{
	public partial class cascadeForm: DevExpress.XtraEditors.XtraForm
	{
        public static cascadeForm instance = new cascadeForm();
        public cascadeForm()
		{
            instance = this;
            InitializeComponent();
		}

        private void cascadeForm_Load(object sender, EventArgs e)
        {
            simpleButton1.Text = SystemInfo.readFactoryName;
            //加载部门
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            formLevel = 0;
            formParentDepID = 0;
            this.Close();
            //开始加载新Form事件
        }

        private void InitButtons(DataTable dt, XtraScrollableControl pl,string Col)
        {
            //在Panel中添加button
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string btnText = dt.Rows[i][Col].ToString();
                int buttonWidth = 100;    // 按钮的宽度
                int buttonHeight = 70;    // 按钮的高度
                int buttonSpacing = 30;   // 按钮之间的间距

                SimpleButton btn = new SimpleButton();
                // 设置按钮的尺寸88
                btn.Width = buttonWidth;
                btn.Height = buttonHeight;
                btn.BackColor = Color.FromArgb(30, 42, 99);
                btn.Tag = (int)dt.Rows[i]["ID"];

                // 设置按钮的位置，每个按钮的X坐标依次增加
                btn.Location = new Point(i * (buttonWidth + buttonSpacing) + 12, 14); // 水平排列，Y坐标保持为0
                btn.Text = btnText;
                //btn.Click += (s, ev) => FactoryButtonClick(btn);

                pl.Controls.Add(btn);
            }
        }

        private int formLevel = 0;
        private string formParentDep = "";
        private int formParentDepID = 0;

        private void DepartmentButtonClick(SimpleButton btn)
        {
            plSubDepartment.Controls.Clear();
            DataTable res = GetSubDepartment(Convert.ToInt32(btn.Tag));

            InitButtons(res, plSubDepartment, "DepartmentName");

            formLevel = publicFunc.MaxDepartmentLevel() - 1;
            formParentDepID = Convert.ToInt32(btn.Tag);
            formParentDep = btn.Text;

            foreach (Control controls in plSubDepartment.Controls)
            {
                if (controls is SimpleButton subbtn)
                {
                    subbtn.Click += (s, ev) => SubDepartmentButtonClick(subbtn);
                }
            }

        }

        private void ButtomDepButtonClick(SimpleButton btn)
        {
            formLevel = publicFunc.MaxDepartmentLevel() - 3;
            formParentDepID = Convert.ToInt32(btn.Tag);
            formParentDep = btn.Text;
            Console.WriteLine(formLevel + ", " + formParentDepID);
        }

        private void SubDepartmentButtonClick(SimpleButton btn)
        {
            plButtomDep.Controls.Clear();
            DataTable res = GetSubDepartment(Convert.ToInt32(btn.Tag));
            InitButtons(res, plButtomDep, "DepartmentName");

            formLevel = publicFunc.MaxDepartmentLevel() - 2;
            formParentDepID = Convert.ToInt32(btn.Tag);
            formParentDep = btn.Text;
            Console.WriteLine(formLevel + ", " + formParentDepID);

            foreach (Control controls in plButtomDep.Controls)
            {
                if (controls is SimpleButton subbtn)
                {
                    subbtn.Click += (s, ev) => ButtomDepButtonClick(subbtn);
                }
            }
        }
        private DataTable GetDepartment(int depLevel)
        {
            DataTable dtDep = new DataTable();
            dtDep = SQL.ReturnDataTable3(SystemInfo.SQL,"Departments",$"Del_tag = 0 and isDisplay = 1 and departmentLevel = {depLevel}");
            if (dtDep.Rows.Count > 0)
            {
                return dtDep;
            }
            else
            {
                //XtraMessageBox.Show("Cannot find any factories in Database");
                return dtDep;
            }
        }

        private DataTable GetSubDepartment(int ParentDep)
        {
            DataTable dtDep = new DataTable();
            dtDep = SQL.ReturnDataTable3(SystemInfo.SQL, "Departments", $"Del_tag = 0 and isDisplay = 1 and ParentDepartmentID = {ParentDep}");
            if (dtDep.Rows.Count > 0)
            {
                return dtDep;
            }
            else
            {
                //XtraMessageBox.Show("Cannot find any factories in Database");
                return dtDep;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //工厂按钮
            plDepartment.Controls.Clear();
            formLevel = publicFunc.MaxDepartmentLevel();
            formParentDepID = 0;

            InitButtons(GetDepartment(publicFunc.MaxDepartmentLevel()), plDepartment, "DepartmentName");
            foreach (Control controls in plDepartment.Controls)
            {
                if (controls is SimpleButton btn)
                {
                    btn.Click += (s, ev) => DepartmentButtonClick(btn);
                }
            }

        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            SystemInfo.readLevel = formLevel;
            SystemInfo.readparentDep = formParentDepID;
            if(formParentDepID == 0)
            {
                formParentDep = SystemInfo.readFactoryName;
            }
            SystemInfo.readparentDepName = formParentDep;
            DevMainForm.instance.LoadinOverview();
            DevMainForm.instance.btnOverview_Click(sender, e);
            this.Close();

        }
    }
}